package Q59

class Main {

}

fun main() {

}

class Solution {
    fun generateMatrix(n: Int): Array<IntArray> {
        val arr = Array(n) { IntArray(n) }
        var c = 1
        var j = 0
        while (c <= n * n) {
            for (i in j until n - j) arr[j][i] = c++
            for (i in j + 1 until n - j) arr[i][n - j - 1] = c++
            for (i in n - j - 2 downTo j) arr[n - j - 1][i] = c++
            for (i in n - j - 2 downTo j + 1) arr[i][j] = c++
            j++
        }
        return arr
    }
}